#include "Speedrun_Bar.h"

#define PROCESSBAR_TAG 1

Speedrun_Bar::Speedrun_Bar(void)
{
	_processBar = NULL;
	_percent = 0;
}


Speedrun_Bar::~Speedrun_Bar(void)
{
	CC_SAFE_RELEASE(_processBar);
}

bool Speedrun_Bar::init()
{
	if (!CCNode::init())
		return false;

	int width = 200;
	
	CCSprite* border = CCSprite::createWithSpriteFrameName("speedrun_border.png");
	border->setAnchorPoint(CCPointZero);
	border->setPosition(ccp(47, 7));
	border->setScaleX(1);
	addChild(border, 0);

	

	CCSprite* process = CCSprite::createWithSpriteFrameName("speedrun_process.png");
	process->setAnchorPoint(CCPointZero);
	process->setPosition(CCPointZero);
	process->setFlipY(true);
	
	CCRenderTexture* textRen = CCRenderTexture::create(process->getContentSize().width, process->getContentSize().height);
	//textRen->beginWithClear(255, 255, 255, 255);
	textRen->begin();
	process->visit();
	textRen->end();

	//process->setPosition(ccp(47, 7));
	//addChild(textRen);

	setProcessBar(textRen->getSprite()->getTexture());

	CCSprite* icon = CCSprite::createWithSpriteFrameName("speedrun_icon.png");
	icon->setAnchorPoint(CCPointZero);
	addChild(icon, 1);

// 	CCSprite* bar = CCSprite::createWithTexture(_processBar);
// 	bar->setAnchorPoint(CCPointZero);
// 	bar->setPosition(ccp(47, 7));
// 	bar->setScaleY(1);
	//addChild(bar, 0, PROCESSBAR_TAG);
	setPercent(100);
	return true;
}

float Speedrun_Bar::getPercent()
{
	return _percent;
}

void Speedrun_Bar::setPercent(float var)
{
	if (var < 0)
		var = 0;
	if (var > 100)
		var = 100;
	_percent = var;

	// 240 40
	removeChildByTag(PROCESSBAR_TAG);
	CCRect rect = CCRectMake(240 * _percent / 100, 0, 240 - 240 * _percent / 100, 40);
	CCSprite* bar = CCSprite::createWithTexture(_processBar, rect);
	bar->setAnchorPoint(CCPointZero);
	bar->setPosition(ccp(47, 7));
	addChild(bar, 0, PROCESSBAR_TAG);
}